<?php

namespace Apps\AppApi\Method\User;


use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\MobileSMS;
use Apps\Common\Models\UserBase;
use PhalconPlus\Api\Common\ResultStatus;

class SetBasicInfo extends BaseMethod
{
    public function run()
    {
        if(false == $this->verifyUserAuth()){
            return false;
        }

        $mobile = $this->getDataItem('mobile', '');//帐号
        $smsCode = $this->getDataItem('sms_code', '');//短信验证
        $password = $this->getDataItem('password', '');

        if (empty($mobile) || empty($smsCode) || empty($password)) {
            return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, '缺少必传参数或者参数格式错误！');
        }

        if($this->userInfoByAccount($mobile)){
            $this->setResult([
                'success' => 100,
                'message' => '手机号码已经使用过了.'
            ]);
            return false;
        }
        if(!MobileSMS::Check('update_mobile', $smsCode, $mobile, $this->apiAccessToken->id)) {
            $this->setResult([
                'success' => 101,
                'message' => '短信验证码错误'
            ]);
            return false;
        }

        $UserBase = UserBase::findFirst('user_id = ' . $this->getUserAuth()->userId);

        if ($UserBase->user_account) {
            return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, '已经绑定过的账号无法初始化账号信息！');
        }

        $UserBase->user_account = $mobile;
        $UserBase->bind_phone = $mobile;
        $UserBase->user_password = $this->hashPassword($password);

        if ($UserBase->save()) {
            $this->setResult([
                'success' => 1,
                'message' => '绑定手机号码成功.'
            ]);
        } else {
            $this->setResult([
                'success' => 0,
                'message' => '保存数据异常.'
            ]);
        }
    }


}